package com.blockmeta.bbs.immodule.utils;

import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;

import com.blankj.utilcode.util.LogUtils;
import com.blockmeta.bbs.immodule.Constants;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.broadcast.IMDataReceiver;
import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.dao.Todo;
import com.blockmeta.bbs.immodule.service.Session;

import org.jetbrains.annotations.NotNull;


public class BroadcastHelper {
    @NotNull
    private static Intent getIntent(Session context, String action) {
        Intent intent0 = new Intent();
        intent0.setClass(context, IMDataReceiver.class);
        intent0.setAction(action);
        return intent0;
    }

    public static void onSendChatMessage(Session context) {
        Intent intent0 = getIntent(context, Constants.INTENT_ACTION_SEND_CHAT_MESSAGE);
        sendBroadcast(context, intent0);
    }

    /**
     * 发送语音消息通知
     *
     * @param file
     * @param context
     */
    // public static void sendVoiceMsg(Attachment file, Session context, int
    // toId) {
    // Intent intent0 = new Intent();
    // intent0.setAction(Constants.INTENT_ACTION_SEND_VOICE_MESSAGE);
    // Bundle bundle = new Bundle();
    // bundle.putSerializable(Constants.INTENT_EXTRA_VOICE_MESSAGE, file);
    // bundle.putInt(Constants.INTENT_EXTRA_TO_USER_ID, toId);
    // intent0.putExtras(bundle);
    // sendBroadcast(context, intent0);
    // }

    /**
     * 收到消息
     *
     * @param context
     */
    public static void onReceiveChatMessageList(Session context) {


        Intent intent0 = getIntent(context, Constants.INTENT_ACTION_RECEIVE_CHAT_MESSAGE_LIST);
        sendBroadcast(context, intent0);
    }

    /**
     * 收到代办消息
     *
     * @param context
     */
    public static void onReceiveTodoList(Session context) {
        Intent intent0 = getIntent(context, Constants.INTENT_ACTION_RECEIVE_TODO_LIST);
        sendBroadcast(context, intent0);
    }

    private static void sendBroadcast(Session context, Intent intent0) {
        //采用aidl回调返回数据
        int i = 0;
        while (context.getIMCallBack() == null && i < 20){
            try {
                LogUtils.i("authorkkk" + "sendBroadcast sleep"  + i);
                i++;
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            LogUtils.i("authorkkk" + "sendBroadcast "  + intent0.getAction());
            context.getIMCallBack().onReceiveData(intent0);
        } catch (RemoteException e) {
            LogUtils.i("authorkkk" + "sendBroadcast error"  + e.getMessage());
            e.printStackTrace();
        }
        //采用service绑定返回
//        try {
//            context.getIMClientService().onReceiveData(intent0);
//        } catch (RemoteException e) {
//            e.printStackTrace();
//        }
    }

    public static void onReceiveReceiptMessage(Session context, String uuid, int status) {
        Intent intent0 = getIntent(context, Constants.INTENT_ACTION_RECEIVE_RECEIPT_MESSAGE);
        Bundle bundle = new Bundle();
        bundle.putString(Params.RECEIPT_UUID,
                uuid);
        bundle.putInt(Params.RECEIPT_STATUS,status);
        intent0.putExtras(bundle);
        sendBroadcast(context, intent0);
    }

    public static void onReceiveLoginMessage(Session context, boolean success) {
        Intent intent0 = getIntent(context, Constants.INTENT_ACTION_RECEIVE_LOGIN_MESSAGE);
        Bundle bundle = new Bundle();
        bundle.putBoolean(Params.LOGIN_STATUS,success);
        intent0.putExtras(bundle);
        sendBroadcast(context, intent0);
    }

    public static void onReceiveLiveChatMessage(Session context, ChatMessage chatMessage) {
        Intent intent0 = getIntent(context, Constants.INTENT_ACTION_RECEIVE_CHAT_MESSAGE);
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constants.INTENT_EXTRA_CHAT_MESSAGE,
                chatMessage);
        intent0.putExtras(bundle);
        sendBroadcast(context, intent0);

    }

    public static void onReceiveChatGroupList(Session context) {
        Intent intent0 = getIntent(context, Constants.INTENT_ACTION_RECEIVE_FRIEND_GROUP_LIST);
        sendBroadcast(context, intent0);
    }

    public static void onReceiveFriendsList(Session context) {
        Intent intent0 = getIntent(context, Constants.INTENT_ACTION_RECEIVE_FRIEND_LIST);
        sendBroadcast(context, intent0);
    }

    public static void onReceiveLiveTodo(Session context, Todo todo) {
        Intent intent0 = getIntent(context, Constants.INTENT_ACTION_RECEIVE_TODO);
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constants.INTENT_EXTRA_TODO,
                todo);
        intent0.putExtras(bundle);
        sendBroadcast(context, intent0);
    }

    public static void sendFriendAddEvent(Session context) {
        sendBroadcast(context, getIntent(context, Constants.INTENT_ACTION_ADD_FRIENDS));
    }


    public static void sendFriendDeleteEvent(Session context) {
        sendBroadcast(context, getIntent(context, Constants.INTENT_ACTION_DELETE_FRIENDS));
    }

    public static void sendCircleDeleteEvent(Session context) {
        sendBroadcast(context, getIntent(context, Constants.INTENT_ACTION_DELETE_CIRCLE));
    }

    public static void sendCircleAddEvent(Session context) {
        sendBroadcast(context, getIntent(context, Constants.INTENT_ACTION_ADD_CIRCLE));
    }
}
